/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author ademar.concrete
 */
public class DBDescriptor implements Serializable {

    private String porta = "";
    private String host = "";
    private String senha = "";
    private String usuario = "";
    private Boolean ultimaConexao = false;

    public String getPorta() {
        return porta;
    }

    public void setPorta(String porta) {
        this.porta = porta;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getSenha() {
        return senha;
    }

    public void setSenha(String senha) {
        this.senha = senha;
    }

    public String getUsuario() {
        return usuario;
    }

    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }

    public Boolean getUltimaConexao() {
        return ultimaConexao;
    }

    public void setUltimaConexao(Boolean ultimaConexao) {
        this.ultimaConexao = ultimaConexao;
    }

    public void salvar() {
        try {

            List<DBDescriptor> dbs = list();
            boolean find = dbs.size() == 0 ? true : false;
            for (Iterator<DBDescriptor> it = dbs.iterator(); it.hasNext();) {
                DBDescriptor dBDescriptor = it.next();
                if (dBDescriptor.equals(this)) {
                    find = true;
                }
            }
            if (find) {
                dbs.add(this);
            }
            FileOutputStream fo = new FileOutputStream("dbConn.ser");
            ObjectOutputStream oo = new ObjectOutputStream(fo);
            oo.writeObject(dbs);
            oo.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static List<DBDescriptor> list() {
        try {
            FileInputStream fi = new FileInputStream("dbConn.ser");
            ObjectInputStream oi = new ObjectInputStream(fi);
            List<DBDescriptor> dbs = (List<DBDescriptor>) oi.readObject();
            oi.close();
            return dbs;
        } catch (Exception e) {
            return new ArrayList<DBDescriptor>();
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof DBDescriptor) {
            DBDescriptor db = (DBDescriptor) obj;
            return porta.equals(db.getPorta()) && senha.equals(db.getSenha()) && host.equals(db.getHost()) && usuario.equals(db.getUsuario());
        }
        return false;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 79 * hash + (this.porta != null ? this.porta.hashCode() : 0);
        hash = 79 * hash + (this.host != null ? this.host.hashCode() : 0);
        hash = 79 * hash + (this.senha != null ? this.senha.hashCode() : 0);
        hash = 79 * hash + (this.usuario != null ? this.usuario.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return host + " : " + porta + "[" + usuario + "]";
    }
}
